package cn.itsource.hrm.threadTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 */
public class ThreadTest_2 {

    public static void main(String[] args) throws InterruptedException {
        Long start = System.currentTimeMillis();
        final Random random = new Random();
        final List<Integer> list = new ArrayList<>();

        //创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for(int i=1; i<=10000; i++){
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    list.add(random.nextInt());
                }
            });
        }
        executorService.shutdown();//关闭任务
        executorService.awaitTermination(1, TimeUnit.DAYS);//关闭线程池执行器
        Long end = System.currentTimeMillis();
        System.out.println("运行时间：" + (end-start) + "毫秒");//62
        System.out.println("List大小：" + list.size());
    }

}